const app = getApp()
import BaseModel from '../../utils/common.js'

var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    earlyMes:"",//早会信息
    userOrder:"",  //早会修改表单信息
    phoneNumber:"", //分总电话
    tutor_name:"待分配",//讲师名字
    date: '2018-10-01',
    earlyTime:"",//早会时间
    /* dateTimeArray: null,*/
    /*dateTime: null,*/

    dateTimeArray1: null, //全部数组时间日期
    dateTime1: null, //当前选中时间日期
    startYear: 2000, //开始年份
    endYear: 2050 //结束年份
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getEarlyOrderData();
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    console.log(arr)
    var earlyTime= dateArr[0][arr[0]]+'-'+dateArr[1][arr[1]]+'-'+dateArr[2][arr[2]]+' '+dateArr[3][arr[3]]+':'+dateArr[4][arr[4]]
    this.setData({
      earlyTime:earlyTime
    });
  },
  //获取早会详情
  getEarlyOrderModel:new BaseModel({
    url: "detailsMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEarlyOrderData(){
    this.getEarlyOrderModel
        .fetch({
          morningId:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            earlyMes:res.data,
            phoneNumber:res.data.total_phone,
            earlyTime:res.data.time,
            tutor_name:res.data.tutor_name,
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*撤回早会*/
  withdraw:function(){
    var that = this;
    wx.showModal({
      title: '确定要撤回此次早会上报申请吗',
      content: "",
      success(res) {
        if (res.confirm) {
          that.getWithdrawData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //撤回早会提交信息
  getWithdrawModel:new BaseModel({
    url: "deleteMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getWithdrawData(){
    this.getWithdrawModel
        .fetch({
          morningId :this.data.id
        })
        .then(res => {
          //成功
          wx.navigateBack({
            delta:1
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*修改早会信息*/
  formSubmit: function(e) {
    if(e.detail.value.contacts==""){
      wx.showToast({
        title: '联系人未填写',
        icon: 'none'
      })
      return false;
    }else if(!(/^1[3456789]\d{9}$/.test(e.detail.value.phone))){
      wx.showToast({
        title: '电话格式不正确',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.company==""){
      wx.showToast({
        title: '公司未填写',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.address==""){
      wx.showToast({
        title: '地址未填写',
        icon: 'none'
      })
      return false;
    }else if(e.detail.value.estimated_number==""){
      wx.showToast({
        title: '人数未填写',
        icon: 'none'
      })
      return false;
    }else if(!(/^\d+$/.test(e.detail.value.estimated_number))){
      wx.showToast({
        title: '人数只能填写数字',
        icon: 'none'
      })
      return false;
    }
    e.detail.value["time"] = this.data.earlyTime;
    this.setData({
      userOrder: e.detail.value
    })
    this.getUserOrderData();

  },
  //提交修改早会信息
  getUserOrderModel:new BaseModel({
    url: "modifyMorning",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getUserOrderData(){
    this.getUserOrderModel
        .fetch({
          morningId:this.data.id,
          morning:this.data.userOrder
        })
        .then(res => {
          //成功
          wx.navigateBack({
            delta:1
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*拨打分总电话*/
  total_phone:function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phoneNumber // 仅为示例，并非真实的电话号码
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getEarlyOrderData();
    wx.stopPullDownRefresh(); //停止下拉
  },

})